package com.example.ffepeg.ffeAdder;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

//添加字幕list
public class AddSrtFile {
    public static void main(String[] args) {
        String filePath = "src/main/resources/file/example.srt";

        List<String> subtitles = new ArrayList<>();
        // 要写入的字幕内容
        String subStr = "This is the second subtitle";
        int fu = 6;
        for (int i = 1; i < 4; i++) {
            int hu = 1;
            int fui = fu*i;
            if (fui != 6){
                hu = fui-5;
            }
            addSubtitle(subtitles,i,"00:00:"+hu+",000", "00:00:"+fui+",000", subStr);
        }

        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
            for (String subtitle : subtitles) {
                writer.write(subtitle);
                writer.newLine(); // 确保每个字幕条目之间有空行
            }
            System.out.println("所有字幕已成功写入到 " + filePath);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    // 辅助方法用于构建并添加单个字幕条目
    private static void addSubtitle(List<String> subtitles, int index, String startTime, String endTime, String text) {
        StringBuilder subtitle = new StringBuilder();
        subtitle.append(index).append("\n")
                .append(startTime).append(" --> ").append(endTime).append("\n")
                .append(text).append("\n");
        subtitles.add(subtitle.toString());
    }

}
